package com.axonactive.yolo.util.session;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import com.axonactive.yolo.domain.User;
import com.axonactive.yolo.domain.UserEmail;
//import com.axonactive.yolo.dto.Airline;
//import com.axonactive.yolo.dto.AirlinesFilter;
import com.axonactive.yolo.dto.Flight;
import com.axonactive.yolo.dto.Parameters;
import com.axonactive.yolo.dto.ParametersFilter;

public class SessionHelper {
	public static final boolean DEFAULT_BOOLEAN_NOT_EXIST = false;

	static final String USER_EMAIL = "userEmail";
	static final String FLIGHT_LIST = "flights";
	static final String TEMP_FLIGHT = "tempFlight";
	static final String CURRENCY_CODE = "currency_code";
	static final String AIRLINE_NAME_LIST = "airlineNamesList";
	static final String JSONCONTROLLER = "jsonController";
	static final String LOGIN_BACK_TO_SEARCH_RESULT = "loginBackToSearchResult";

	static final String SAVE_USERID = "saveUserId";
	static final String SAVE_USEREMAILID = "saveUserEmailId";
	static final String SAVE_TASKID = "saveTaskId";
	static final String SAVE_TASK_USER_ID = "saveTaskUserId";
	static final String SAVE_LINK_PARAM = "saveLinkParam";
	static final String SAVE_RESET_REQUEST_ACCEPTED = "resetRequestAccepted";
	static final String SAVE_ALREADY_ADDED_EMAIL_SECONDARY = "saveAlreadyAddedEmailSecondary";
	static final String SAVE_EMAIL_PRIMARY = "saveEmailPrimary";
	static final String SAVE_SORTED_INDEXES = "sortedIndexes";
	static final String SAVE_PARAMETERS = "saveParameters";
	static final String SAVE_PARAMETERS_FILTER = "saveParametersFilter";

	static final String GO_BACK_TASKPAGE = "goBackTaskpage";
	static final String GO_BACK_SUBSCRIBE = "goBackSubscribe";
	static final String GO_BACK_NOTY = "goBackNoTy";
	
	static final String EXCHANGE_RATES = "exchangeRates";

	private static final String GO_BACK_SEARCH_PAGE = "goBackSearchPage";

	private static final String TRANSFER_SEARCH_CRITERIA = "transferSearchCriteria";

	private static final String TRANSFER_SEARCH_CRITERIA_RESULT = "transferSearchCriteriaResult";

	private static final String JSON_FLIGHT_DATA = "jsonFlightData";

	private static final String LOAD_FROM_FLIGHT_SEARCH = "loadFromFlightSearch";

	private static final String MARK_SEARCH = "markSearch";

	public static boolean isLoggedIn(HttpSession session) {
		return getUserEmail(session) != null; // by through UserEmail in session
	}

	/**
	 * Return -1 if user not exist
	 * 
	 * @param session
	 * @return
	 */
	public static int getUserId(HttpSession session) {
		if (session.getAttribute(USER_EMAIL) == null) {
			return -1;
		}
		return ((UserEmail) session.getAttribute(USER_EMAIL)).getUser().getId();
	}

	/**
	 * Return null if user not exist
	 * 
	 * @param session
	 * @return
	 */
	public static User getUser(HttpSession session) {
		if (session.getAttribute(USER_EMAIL) == null) {
			return null;
		}
		return ((UserEmail) session.getAttribute(USER_EMAIL)).getUser();
	}

	public static void setUser(HttpSession session, User user) {
		if (session.getAttribute(USER_EMAIL) == null) {
			return;
		}
		UserEmail userEmail = (UserEmail) session.getAttribute(USER_EMAIL);
		userEmail.setUser(user);
		session.setAttribute(USER_EMAIL, userEmail);
	}

	/**
	 * Return '' if currencyCode not exist
	 * 
	 * @param session
	 * @return
	 */
	public static String getCurrencyCode(HttpSession session) {
		if (session.getAttribute(CURRENCY_CODE) == null) {
			return "";
		}
		return (String) session.getAttribute(CURRENCY_CODE);
	}

	public static void setCurrencyCode(HttpSession session, String currencyCode) {
		session.setAttribute(CURRENCY_CODE, currencyCode);
	}

	public static void removeCurrencyCode(HttpSession session) {
		session.removeAttribute(CURRENCY_CODE);
	}
	
	/**
	 * Return '' if LoginBackToSearchResult not exist
	 * 
	 * @param session
	 * @return
	 */
	public static String getLoginBackToSearchResult(HttpSession session) {
		if (session.getAttribute(LOGIN_BACK_TO_SEARCH_RESULT) == null) {
			return "";
		}
		return (String) session.getAttribute(LOGIN_BACK_TO_SEARCH_RESULT);
	}

	public static void setLoginBackToSearchResult(HttpSession session, String loginBackToSearchResult) {
		session.setAttribute(LOGIN_BACK_TO_SEARCH_RESULT, loginBackToSearchResult);
	}

	public static void removeLoginBackToSearchResult(HttpSession session) {
		session.removeAttribute(LOGIN_BACK_TO_SEARCH_RESULT);
	}
	
	/**
	 * Return '' if jsonController not exist
	 * 
	 * @param session
	 * @return
	 */
	public static String getJsonController(HttpSession session) {
		if (session.getAttribute(JSONCONTROLLER) == null) {
			return "";
		}
		return (String) session.getAttribute(JSONCONTROLLER);
	}

	public static void setJsonController(HttpSession session, String jsonController) {
		session.setAttribute(JSONCONTROLLER, jsonController);
	}

	public static void removeJsonController(HttpSession session) {
		session.removeAttribute(JSONCONTROLLER);
	}

	/**
	 * FOR SAVE USER INFO WHEN LOGIN: Return null if userEmail not exist
	 * 
	 * @param session
	 * @return
	 */
	public static UserEmail getUserEmail(HttpSession session) {
		if (session.getAttribute(USER_EMAIL) == null) {
			return null;
		}
		return (UserEmail) session.getAttribute(USER_EMAIL);
	}

	public static void setUserEmail(HttpSession session, UserEmail userEmail) {
		session.setAttribute(USER_EMAIL, userEmail);
	}

	public static void removeUserEmail(HttpSession session) {
		session.removeAttribute(USER_EMAIL);
	}

	/**
	 * Return null if tempFlight not exist
	 * 
	 * @param session
	 * @return
	 */
	public static Flight getTempFlight(HttpSession session) {
		if (session.getAttribute(TEMP_FLIGHT) == null) {
			return null;
		}
		return (Flight) session.getAttribute(TEMP_FLIGHT);
	}

	public static void setTempFlight(HttpSession session, Flight tempFlight) {
		session.setAttribute(TEMP_FLIGHT, tempFlight);
	}

	public static void removeTempFlight(HttpSession session) {
		session.removeAttribute(TEMP_FLIGHT);
	}

	/**
	 * Return null if tempParameters not exist
	 * 
	 * @param session
	 * @return
	 */
	public static Parameters getSaveParameters(HttpSession session) {
		if (session.getAttribute(SAVE_PARAMETERS) == null) {
			return null;
		}
		return (Parameters) session.getAttribute(SAVE_PARAMETERS);
	}

	public static void setSaveParameters(HttpSession session,
			Parameters tempParameters) {
		session.setAttribute(SAVE_PARAMETERS, tempParameters);
	}

	public static void removeSaveParameters(HttpSession session) {
		session.removeAttribute(SAVE_PARAMETERS);
	}

	/**
	 * Return null if tempParametersFilter not exist
	 * 
	 * @param session
	 * @return
	 */
	public static ParametersFilter getSaveParametersFilter(HttpSession session) {
		if (session.getAttribute(SAVE_PARAMETERS_FILTER) == null) {
			return null;
		}
		return (ParametersFilter) session.getAttribute(SAVE_PARAMETERS_FILTER);
	}

	public static void setSaveParametersFilter(HttpSession session,
			ParametersFilter tempParametersFilter) {
		session.setAttribute(SAVE_PARAMETERS_FILTER, tempParametersFilter);
	}

	public static void removeSaveParametersFilter(HttpSession session) {
		session.removeAttribute(SAVE_PARAMETERS_FILTER);
	}

	/**
	 * Return false if goBackTaskpage not exist
	 * 
	 * @param session
	 * @return
	 */
	public static boolean isGoBackTaskpage(HttpSession session) {
		if (session.getAttribute(GO_BACK_TASKPAGE) == null) {
			return DEFAULT_BOOLEAN_NOT_EXIST;
		}
		return (Boolean) session.getAttribute(GO_BACK_TASKPAGE);
	}

	public static void setGoBackTaskpage(HttpSession session,
			boolean goBackTaskpage) {
		session.setAttribute(GO_BACK_TASKPAGE, goBackTaskpage);
	}

	public static void removeGoBackTaskpage(HttpSession session) {
		session.removeAttribute(GO_BACK_TASKPAGE);
	}

	/**
	 * Return false if goBackSubscribe not exist
	 * 
	 * @param session
	 * @return
	 */
	public static boolean isGoBackSubscribe(HttpSession session) {
		if (session.getAttribute(GO_BACK_SUBSCRIBE) == null) {
			return DEFAULT_BOOLEAN_NOT_EXIST;
		}
		return (Boolean) session.getAttribute(GO_BACK_SUBSCRIBE);
	}

	public static void setGoBackSubscribe(HttpSession session,
			boolean goBackSubscribe) {
		session.setAttribute(GO_BACK_SUBSCRIBE, goBackSubscribe);
	}

	public static void removeGoBackSubscribe(HttpSession session) {
		session.removeAttribute(GO_BACK_SUBSCRIBE);
	}
	
	/**
	 * Return false if goBackNoty not exist
	 * 
	 * @param session
	 * @return
	 */
	public static boolean isGoBackNoty(HttpSession session) {
		if (session.getAttribute(GO_BACK_NOTY) == null) {
			return DEFAULT_BOOLEAN_NOT_EXIST;
		}
		return (Boolean) session.getAttribute(GO_BACK_NOTY);
	}

	public static void setGoBackNoty(HttpSession session,
			boolean goBackSubNoty) {
		session.setAttribute(GO_BACK_NOTY, goBackSubNoty);
	}

	public static void removeGoBackNoty(HttpSession session) {
		session.removeAttribute(GO_BACK_NOTY);
	}
	
	
	
	/**
	 * Return "" if AirlineNames not exist
	 * 
	 * @param session
	 * @return
	 */
	public static String getAirlinesNameList(HttpSession session) {
		if (session.getAttribute(AIRLINE_NAME_LIST) == null) {
			return "";
		}
		return (String) session.getAttribute(AIRLINE_NAME_LIST);
	}

	public static void setAirlinesNameList(HttpSession session,String airlines) {
		session.setAttribute(AIRLINE_NAME_LIST, airlines);
	}
	
	public static void removeAirlinesNameList(HttpSession session){
		session.removeAttribute(AIRLINE_NAME_LIST);
	}

	/**
	 * Return list with size 0 if filghts not exist
	 * 
	 * @param session
	 * @return
	 */
	public static List<Flight> getFlights(HttpSession session) {
		if (session.getAttribute(FLIGHT_LIST) == null) {
			return new ArrayList<Flight>();
		}
		return (ArrayList<Flight>) session.getAttribute(FLIGHT_LIST);
	}

	public static void setFlights(HttpSession session, List<Flight> flights) {
		session.setAttribute(FLIGHT_LIST, flights);
	}
	
	public static void removeFlights(HttpSession session){
		session.removeAttribute(FLIGHT_LIST);
	}
	
	/**
	 * Return list with size 0 if filghtsFilter not exist
	 * 
	 * @param session
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<Flight> getFlightsFilter(HttpSession session) {
		if (session.getAttribute(FLIGHT_LIST) == null) {
			return new ArrayList<Flight>();
		}
		return (ArrayList<Flight>) session.getAttribute(FLIGHT_LIST);
	}

	public static void setFlightsFilter(HttpSession session, List<Flight> flights) {
		session.setAttribute(FLIGHT_LIST, flights);
	}

	/**
	 * Return -1 if saveUserId not exist
	 * 
	 * @param session
	 * @return
	 */
	public static int getSaveUserId(HttpSession session) {
		if (session.getAttribute(SAVE_USERID) == null) {
			return -1;
		}
		return (int) session.getAttribute(SAVE_USERID);
	}

	public static void setSaveUserId(HttpSession session, int saveUserId) {
		session.setAttribute(SAVE_USERID, saveUserId);
	}

	/**
	 * Return -1 if saveUserEmailId not exist
	 * 
	 * @param session
	 * @return
	 */
	public static int getSaveUserEmailId(HttpSession session) {
		if (session.getAttribute(SAVE_USEREMAILID) == null) {
			return -1;
		}
		return (int) session.getAttribute(SAVE_USEREMAILID);
	}

	public static void setSaveUserEmailId(HttpSession session,
			int saveUserEmailId) {
		session.setAttribute(SAVE_USEREMAILID, saveUserEmailId);
	}

	/**
	 * Return '' if param not exist
	 * 
	 * @param session
	 * @return
	 */
	public static String getSaveLinkParam(HttpSession session) {
		if (session.getAttribute(SAVE_LINK_PARAM) == null) {
			return "";
		}
		return (String) session.getAttribute(SAVE_LINK_PARAM);
	}

	public static void setSaveLinkParam(HttpSession session,
			String saveLinkParam) {
		session.setAttribute(SAVE_LINK_PARAM, saveLinkParam);
	}

	/**
	 * Return false if resetRequestAccepted not exist
	 * 
	 * @param session
	 * @return
	 */
	public static boolean isResetRequestAccepted(HttpSession session) {
		if (session.getAttribute(SAVE_RESET_REQUEST_ACCEPTED) == null) {
			return DEFAULT_BOOLEAN_NOT_EXIST;
		}
		return (Boolean) session.getAttribute(SAVE_RESET_REQUEST_ACCEPTED);
	}

	public static void setResetRequestAccepted(HttpSession session,
			boolean resetRequestAccepted) {
		session.setAttribute(SAVE_RESET_REQUEST_ACCEPTED, resetRequestAccepted);
	}

	/**
	 * Return list with size 0 if saveAlreadyAddedEmailSecondary not exist
	 * 
	 * @param session
	 * @return
	 */
	public static List<String> getSaveAlreadyAddedEmailSecondary(
			HttpSession session) {
		if (session.getAttribute(SAVE_ALREADY_ADDED_EMAIL_SECONDARY) == null) {
			return new ArrayList<String>();
		}
		return (ArrayList<String>) session.getAttribute(SAVE_ALREADY_ADDED_EMAIL_SECONDARY);
	}

	public static void setSaveAlreadyAddedEmailSecondary(HttpSession session,
			List<String> saveAlreadyAddedEmailSecondary) {
		session.setAttribute(SAVE_ALREADY_ADDED_EMAIL_SECONDARY, saveAlreadyAddedEmailSecondary);
	}

	/**
	 * Return '' if param not exist
	 * 
	 * @param session
	 * @return
	 */
	public static String getSaveEmailPrimary(HttpSession session) {
		if (session.getAttribute(SAVE_EMAIL_PRIMARY) == null) {
			return "";
		}
		return (String) session.getAttribute(SAVE_EMAIL_PRIMARY);
	}

	public static void setSaveEmailPrimary(HttpSession session,
			String saveEmailPrimary) {
		session.setAttribute(SAVE_EMAIL_PRIMARY, saveEmailPrimary);
	}

	/**
	 * Return "" if saveTaskId not exist
	 * 
	 * @param session
	 * @return
	 */
	public static String getSaveTaskId(HttpSession session) {
		if (session.getAttribute(SAVE_TASKID) == null) {
			return "";
		}
		return (String) session.getAttribute(SAVE_TASKID);
	}

	public static void setSaveTaskId(HttpSession session, String saveTaskId) {
		session.setAttribute(SAVE_TASKID, saveTaskId);
	}
	
	public static String getSaveTaskUserId(HttpSession session) {
		if (session.getAttribute(SAVE_TASK_USER_ID) == null) {
			return "";
		}
		return (String) session.getAttribute(SAVE_TASK_USER_ID);
	}

	public static void setSaveTaskUserId(HttpSession session, String saveTaskUserId) {
		session.setAttribute(SAVE_TASK_USER_ID, saveTaskUserId);
	}

	/**
	 * Return list with size 0 if saveAlreadyAddedEmailSecondary not exist
	 * 
	 * @param session
	 * @return
	 */
	public static int[][] getSaveSortedIndexes(HttpSession session) {
		if (session.getAttribute(SAVE_SORTED_INDEXES) == null) {
			return new int[3][];
		}
		return (int[][]) session.getAttribute(SAVE_SORTED_INDEXES);
	}

	public static void setSaveSortedIndexes(HttpSession session,
			int[][] sortedIndexes) {
		session.setAttribute(SAVE_SORTED_INDEXES, sortedIndexes);
	}
	public static void removeSortedIndexes(HttpSession session){
		session.removeAttribute(SAVE_SORTED_INDEXES);
	}
	
	public static String getExchangeRates(HttpSession session) {
		if (session.getAttribute(EXCHANGE_RATES) == null) {
			return "";
		}
		return (String) session.getAttribute(EXCHANGE_RATES);
	}

	public static void setExchangeRates(HttpSession session, String exchangeRates) {
		session.setAttribute(EXCHANGE_RATES, exchangeRates);
	}

	public static void setGoBackSearchPage(HttpSession session, boolean isGoBack) {
		session.setAttribute(GO_BACK_SEARCH_PAGE, isGoBack);
	}
	
	public static boolean isGoBackSearchPage(HttpSession session) {
		if (session.getAttribute(GO_BACK_SEARCH_PAGE) == null) {
			return DEFAULT_BOOLEAN_NOT_EXIST;
		}
		return (Boolean) session.getAttribute(GO_BACK_SEARCH_PAGE);
	}

	public static void setTransferFlag(HttpSession session, boolean b) {
		session.setAttribute(TRANSFER_SEARCH_CRITERIA, b);
		
	}
	public static boolean isTransferFlagSet(HttpSession session) {
		if (session.getAttribute(TRANSFER_SEARCH_CRITERIA) == null) {
			return DEFAULT_BOOLEAN_NOT_EXIST;
		}
		return (Boolean) session.getAttribute(TRANSFER_SEARCH_CRITERIA);
	}
	public static void removeTransferFlag(HttpSession session){
		session.removeAttribute(TRANSFER_SEARCH_CRITERIA);
	}

	public static void setCreateQuickTaskResult(HttpSession session, boolean b) {
		session.setAttribute(TRANSFER_SEARCH_CRITERIA_RESULT, b);
	}
	public static boolean isCreateQuickTaskResult(HttpSession session) {
		if (session.getAttribute(TRANSFER_SEARCH_CRITERIA_RESULT) == null) {
			return DEFAULT_BOOLEAN_NOT_EXIST;
		}
		return (Boolean) session.getAttribute(TRANSFER_SEARCH_CRITERIA_RESULT);
	}
	public static void removeCreateQuickTaskResult(HttpSession session){
		session.removeAttribute(TRANSFER_SEARCH_CRITERIA_RESULT);
	}

	public static void setJSONFlights(HttpSession session,String jsonFlights) {
		session.setAttribute(JSON_FLIGHT_DATA,jsonFlights);
	}
	public static String getJSONFlights(HttpSession session) {
		if (session.getAttribute(JSON_FLIGHT_DATA) == null) {
			return "";
		}
		return (String) session.getAttribute(JSON_FLIGHT_DATA);
	}

	public static void setLoadFromFlightSearch(HttpSession session, boolean value) {
		session.setAttribute(LOAD_FROM_FLIGHT_SEARCH, value);
	}
	public static boolean isLoadFromFlightSearch(HttpSession session) {
		if (session.getAttribute(LOAD_FROM_FLIGHT_SEARCH) == null) {
			return DEFAULT_BOOLEAN_NOT_EXIST;
		}
		return (Boolean) session.getAttribute(LOAD_FROM_FLIGHT_SEARCH);
	}
	public static void removeLoadFromFlightSearch(HttpSession session){
		session.removeAttribute(LOAD_FROM_FLIGHT_SEARCH);
	}

	
	public static void markSearched(HttpSession session,boolean value) {
		session.setAttribute(MARK_SEARCH, value);
	}
	public static boolean isSearched(HttpSession session) {
		if (session.getAttribute(MARK_SEARCH) == null) {
			return DEFAULT_BOOLEAN_NOT_EXIST;
		}
		return (Boolean) session.getAttribute(MARK_SEARCH);
	}
	public static void removeSearchFlag(HttpSession session){
		session.removeAttribute(MARK_SEARCH);
	}
}
